package com.print.androidprintproject.page;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
import com.print.androidprintproject.R;
import com.print.androidprintproject.print.PrintJobService;
import com.print.printlib.utils.PrintUtils;

public class TextPrintActivity extends BaseActivity implements OnClickListener, OnCheckedChangeListener {

    private static final String TAG = "TextPrintActivity";
    private LinearLayout header;
    private Button btn_send, btn_print_note;
    private ToggleButton tb_isHexData;
    private EditText et_input;
    private boolean isHexData = false;
    //	private static PrinterInstance mPrinter;
    private PrintJobService printJobService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_text);
        init();
        printJobService = PrintJobService.getObject(null, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void init() {
        et_input = findViewById(R.id.et_input);
        et_input.setText(this.getResources().getText(R.string.textprintactivty_input_content) + "\r\n");
        header = findViewById(R.id.ll_headerview_textPrint);
        btn_send = findViewById(R.id.btn_send);
        btn_send.setOnClickListener(this);
        btn_print_note = findViewById(R.id.btn_print_ticket);
        btn_print_note.setOnClickListener(this);
        btn_send.setOnClickListener(this);
        tb_isHexData = findViewById(R.id.tb_hex_on);
        tb_isHexData.setOnCheckedChangeListener(this);
        isHexData = tb_isHexData.isChecked();
        initHeader();
    }

    /**
     * 初始化标题上的信息
     */
    private void initHeader() {
        setHeaderLeftText(header, getString(R.string.back), new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        headerConnecedState.setText(getTitleState());
        setHeaderCenterText(header, getString(R.string.headview_TextPrint));
        setHeaderLeftImage(header, new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void printText() {
        String content = et_input.getText().toString();
        if(isHexData) {
            byte []btBuff = PrintUtils.stringToHex(content);
            printJobService.printBuffer(btBuff);
        } else {
            printJobService.printText(content);
        }
    }

    private void ticketPrint() {
        printJobService.printTicket();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_send:
                printText();
                break;
            case R.id.btn_print_ticket:
                ticketPrint();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {// 16进制开
            isHexData = true;
            et_input.setText("B4 F2 D3 A1 B2 E2 CA D4 70 72 69 6E 74 20 74 65 73 74 0D 0A");
        } else {
            isHexData = false;
            et_input.setText(this.getResources().getText(R.string.textprintactivty_input_content) + "\r\n");
        }
    }
}
